package com.kayang.common.config;

import com.kayang.common.db.DBDebug;
import com.kayang.nadapt.util.LogHelper;
import com.kayang.nadapt.util.WebConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class InitDB {
    @Autowired
    CommonConfig commonConfig;

    @Autowired
    DbConfig dbConfig;

    @PostConstruct
    public void initDb(){
        Map<String, String> map = new HashMap();
        map.put("debug", commonConfig.getDebug());
        map.put("dbdebug", commonConfig.getDbdebug());

        map.put("dbtype", dbConfig.getDbtype());
        map.put("server", dbConfig.getServer());
        map.put("dbname", dbConfig.getDbname());
        map.put("account", dbConfig.getAccount());
        map.put("password", dbConfig.getPassword());
        map.put("timeout", dbConfig.getTimeout());
        map.put("execTimeout", dbConfig.getExecTimeout());
        map.put("jdbcUrl", dbConfig.getJdbcUrl());
        map.put("initialPoolSize", dbConfig.getInitialPoolSize());
        map.put("minPoolSize", dbConfig.getMinPoolSize());
        map.put("maxPoolSize", dbConfig.getMaxPoolSize());
        map.put("checkoutTimeout", dbConfig.getCheckoutTimeout());
        map.put("idleConnectionTestPeriod", dbConfig.getIdleConnectionTestPeriod());
        map.put("maxIdleTime", dbConfig.getMaxIdleTime());
        map.put("maxConnectionAge", dbConfig.getMaxConnectionAge());
        map.put("autoCommitOnClose", dbConfig.getAutoCommitOnClose());
        map.put("acquireIncrement", dbConfig.getAcquireIncrement());
        map.put("numHelperThreads", dbConfig.getNumHelperThreads());
        map.put("acquireRetryAttempts", dbConfig.getAcquireRetryAttempts());
        map.put("acquireRetryDelay", dbConfig.getAcquireRetryDelay());
        map.put("breakAfterAcquireFailure", dbConfig.getBreakAfterAcquireFailure());
        map.put("maxStatements", dbConfig.getMaxStatements());
        map.put("maxStatementsPerConnection", dbConfig.getMaxStatementsPerConnection());
        map.put("testConnectionOnCheckin", dbConfig.getTestConnectionOnCheckin());


        WebConfig.service = true;
        WebConfig.initServicePerporties(map);

        DBDebug.init();

        LogHelper.info("DB Config Inited OK");

    }
}
